import { get, post } from '@/utils/request';

// 
function browserDownload(data: Blob, filename: string){
    let downloadElement: HTMLAnchorElement = document.createElement('a')
    let uri: string = window.URL.createObjectURL(data)
    downloadElement.href = uri
    downloadElement.download = filename
    document.body.appendChild(downloadElement)
    downloadElement.click()
    document.body.removeChild(downloadElement)
    window.URL.revokeObjectURL(uri)

}


export interface TokenType {
    token: string;
  }
  

export class UserApi {

    static getToken(){
        get<any>('/word-sdk/api/paas/token').then((response: TokenType) => {
            console.log(response);});
    };

    static addUser(){
        post<any>('/users', { name: 'Alice', age: 18 }).then((response: any) => {
            console.log(response);
        });
    }

    static download(){
        get<any>('/word-sdk/api/paas/download', { fileId: '267379005835973633'}, {responseType: 'blob'}).then((response: Blob) => {
            browserDownload(response, "1.docx")
        });
    }
}